package com.hxk.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hxk.pojo.User;
import com.hxk.service.UserService;

@Controller
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/{page}")
	public String goPage(@PathVariable String page) {
		return page;
	}
	
	@RequestMapping("/saveUser")
	public String saveUser(User user) {
		this.userService.addUser(user);
		return "redirect:showUsers";
	}
	
	@RequestMapping("/delUser")
	public String delUser(int id) {
		this.userService.delUser(id);
		return "redirect:showUsers";
	}
	
	@RequestMapping("/updUser")
	public String updUser(User user) {
		this.userService.updUser(user);
		return "redirect:showUsers";
	}
	
	@RequestMapping("/editUser")
	public String editUser(int id, Model model) {
		User user = this.userService.getUserById(id);
		model.addAttribute("user", user);
		return "editUser";
	}
	
	@RequestMapping("/showUsers")
	public String showUsers(User user, Model model) {
		List<User> users = this.userService.listUsers(user);
		model.addAttribute("users", users);
		return "userList";
	}
}
